package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.vo.RepairVO;
import com.group02.community.service.IRepairService;
import com.group02.community.utils.ResponseResult;
import com.group02.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Api
@RestController
public class RepairController {
    @Autowired
    private IRepairService repairService;

    @ApiOperation("查询所有")
    @GetMapping("/repairss")
    public List<RepairVO> getAllRepair(){
        return repairService.selectAll();
    }

    @ApiOperation("条件查询")
    @GetMapping("/repaires")
    public List<RepairVO> getAllOwner(Long ownerId){
        return repairService.selectOwner(ownerId);
    }
    @ApiOperation("查询分页")
    @GetMapping("/repairs/page")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseEntity<IPage<RepairVO>> getRepairPage(Long current,Long size,RepairVO repairVO){
        return ResponseEntity.ok(repairService.getRepairPage(current,size,repairVO));
    }
    @ApiOperation("添加")
    @PostMapping("/repair")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> addRepair(RepairVO repairVO){
        System.out.println(repairVO.toString());
        String time = repairVO.getTime();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time1 = LocalDateTime.parse(time + " 00:00:00", dateTimeFormatter);
        repairVO.setAssignmentTime(time1);
        repairVO.setRepairNum("BX_"+ UUIDUtils.getId());
        repairVO.setCreateTime(LocalDateTime.now());
        repairVO.setRepairState("Pend");
        repairVO.setUserId(repairVO.getOwnerId());
        repairVO.setCommunityId(1338423709557272577L);
        repairService.addRepair(repairVO);
        return  ResponseResult.ok("添加成功");
    }

    @ApiOperation("更新")
    @PutMapping("/repairs")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> updateRepair(@RequestBody RepairVO repairVO){
        if(repairVO.getRepairState()=="Pending"&&repairVO.getReceivingOrdersTime().equals("")){
    repairVO.setReceivingOrdersTime(LocalDateTime.now());
    repairService.updateRepair(repairVO);
    } else if(repairVO.getRepairState()=="Dispatch"&&repairVO.getAssignmentTime().equals("")){
    repairVO.setAssignmentTime(LocalDateTime.now());
    repairService.updateRepair(repairVO);
    }else if(repairVO.getRepairState()=="Pended"&&repairVO.getCompleteTime().equals("")){
    repairVO.setCompleteTime(LocalDateTime.now());
    repairService.updateRepair(repairVO);
    }  else {
    repairService.updateRepair(repairVO);
}

        return ResponseResult.ok("ok");
    }
    @ApiOperation("删除")
    @DeleteMapping("/repair/{repairId}")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> deleteRepair(@PathVariable Long repairId){
        repairService.removeById(repairId);
        return ResponseResult.ok("删除成功");
    }
    @ApiOperation("导出Excel")
    @GetMapping("/repair/download")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseEntity<String> downloadExcel(HttpServletResponse response) throws IOException {
        repairService.downloadExcel(response);
        return ResponseEntity.ok("下载成功");
    }
}
